package com.ruitu.health_1.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.MD5Util;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.arad.view.EditTextCleanView;
import com.ruitu.health_1.R;
import com.ruitu.health_1.mvp.model.LoginModel;
import com.ruitu.health_1.mvp.presenter.LoginPresenter;
import com.ruitu.health_1.view.VerifyCationDialog;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.bean.user.User;
import com.ruitu.router_module.util.ReqUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseActivity<LoginPresenter.LoginP, LoginModel.LoginM> {
    private CheckBox cb_login_mode;
    private CheckBox cb_password_show;
    private LinearLayout ll_password_login;
    private LinearLayout ll_code_login;
    private TextView tv_get_code;
    private EditTextCleanView et_login_tel;
    private EditText et_login_password;
    private EditTextCleanView et_login_password_code;
    private ImageView qq;
    private VerifyCationDialog dialog;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("登录");
        Arad.bus.register(this);
        cb_login_mode = findViewById(R.id.cb_login_mode);
        cb_password_show = findViewById(R.id.cb_password_show);
        ll_password_login = findViewById(R.id.ll_password_login);
        ll_code_login = findViewById(R.id.ll_code_login);
        tv_get_code = findViewById(R.id.tv_get_code);
        et_login_tel = findViewById(R.id.et_login_tel);
        et_login_password = findViewById(R.id.et_login_password);
        et_login_password_code = findViewById(R.id.et_login_password_code);

        qq = findViewById(R.id.qq);

        setListener();
        String tel = Arad.preferences.getString("tel");
        et_login_tel.setText(tel);
        dialog = new VerifyCationDialog(this, 1);
//        dialog.show();

    }

    String tel;
    String password_or_code;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getTel(EventModel eventModel) {
        if (eventModel.getEventCode() == 19110700) {
            et_login_tel.setText((String) eventModel.getEventObj());
        }
    }


    //登录
    public void onLogin(View view) {
        Map<String, String> map = new HashMap<>();
        tel = et_login_tel.getText().toString();
        if (!cb_login_mode.isChecked()) {
            password_or_code = et_login_password.getText().toString();
            if (tel.length() < 11) {
                ToastUtils.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(password_or_code)) {
                ToastUtils.showShort("密码不能为空");
                return;
            }
            map.put("phone", tel);
//            map.put("password", password_or_code);
            map.put("password", MD5Util.md5String(password_or_code));
            showProgress();
            p.login(ReqUtil.getBody(map, true));
        } else {
            password_or_code = et_login_password_code.getText().toString();
            if (tel.length() < 11) {
                ToastUtils.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(password_or_code)) {
                ToastUtils.showShort("验证码不能为空");
                return;
            }
            map.put("phone", tel);
            map.put("yzm", password_or_code);
            showProgress();
            p.loginyan(ReqUtil.getBody(map, true));
        }
    }

    //登陆成功的回调
    public void loginSuccess(User user) {
        Arad.bus.post(new EventModel(19110400, user));
        Arad.preferences.putString("tel", tel).flush();
        finish();
    }

    //验证码获取成功的回调
    public void getCodeSuccess() {
        timer.start();
    }

    //注册
    public void onRegister(View view) {
        startActivity(RegisterActivity.class);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_get_code == v) {
            Map<String, String> map = new HashMap<>();
            tel = et_login_tel.getText().toString();
            map.put("paramOne", tel);
            map.put("paramTwo", "1");
            showProgress();
            p.doyzm(ReqUtil.getBody(map, true));
        }
        if (qq == v) {
            startActivity(ThirdLoginActivity.class);
        }
    }

    private long getTime = 60000;
    private CountDownTimer timer = new CountDownTimer(getTime, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            T(millisUntilFinished / 1000 + "s后获取", false, getResources().getColor(R.color.text_gray));
        }

        @Override
        public void onFinish() {
            T("发送验证码", true, getResources().getColor(R.color.text_blue));
        }
    };

    private void T(String time, boolean i, int color) {
        tv_get_code.setText(time);
        tv_get_code.setEnabled(i);
        tv_get_code.setTextColor(color);
    }

    //忘记密码
    public void onForgetPassword(View view) {
        startActivity(new Intent(this, RegisterActivity.class).putExtra("from", "forget"));
    }

    private void setListener() {
        setOnClickListener(tv_get_code, qq);
        cb_login_mode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cb_login_mode.setText("密码登录");
                    ll_code_login.setVisibility(View.VISIBLE);
                    ll_password_login.setVisibility(View.GONE);
                } else {
                    cb_login_mode.setText("短信验证码登录");
                    ll_password_login.setVisibility(View.VISIBLE);
                    ll_code_login.setVisibility(View.GONE);
                }
            }
        });
        cb_password_show.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    et_login_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    et_login_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Arad.bus.unregister(this);
    }
}
